package com.cuz.hot100.datastruck;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public String convertToString() {
        ListNode temp = this;
        StringBuilder res = new StringBuilder();
        while (temp != null) {
            res.append(temp.val);
            if (temp.next != null) {
                res.append("->");
            }
            temp = temp.next;
        }
        return res.toString();
    }

    public void print() {
        System.out.println(convertToString());
    }

    public static ListNode createByStr(String str) {
        String[] allNode = str.split(",");
        int[] ints = new int[allNode.length];
        for (int index = 0; index < allNode.length; index++) {
            ints[index]= Integer.parseInt(allNode[index]);
        }
        return creatBy(ints);
    }
    public static ListNode creatBy(int...values){
        if (values==null||values.length==0){
            return null;
        }
        ListNode head=new ListNode(values[0]);
        ListNode pre=head;
        for (int index = 1; index < values.length; index++) {
            pre.next=new ListNode(values[index]);
            pre=pre.next;
        }
        return head;
    }
}
